//
//  WD_TimeLineViewController.m
//  BUDING
//
//  Created by HanWoonYong on 16/3/31.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_TimeLineViewController.h"
#import "WD_TableViewTimeLine.h"
#import "WD_TableViewTwo.h"
#import "WD_TimeLineModel.h"
#import "WD_OwnerModel.h"
#import "WD_RelatedModel.h"
#import "WD_EpModel.h"
#import "WD_imageModel.h"
#import "WD_ZiLiaoViewController.h"
#import "WD_ShowImageViewController.h"

@interface WD_TimeLineViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayDataSource;
@property (nonatomic, copy) NSString *strUrl;
@property (nonatomic, strong) NSMutableArray *arrayUrl;
@property (nonatomic, strong) NSMutableArray *arrayEp;
@property (nonatomic, strong) NSMutableArray *relateArray;
@property (nonatomic, strong) WD_TimeLineModel *model;
@property (nonatomic, strong) NSMutableArray *arrayImage;
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, strong) NSMutableArray *mutable;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) WD_RelatedModel *modelRelat;
@end

@implementation WD_TimeLineViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(action)];
    
    [self creatView];
    [self creatData];
}

- (void)creatView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor yellowColor];
    [self.tableView registerClass:[WD_TableViewTimeLine class] forCellReuseIdentifier:@"WD_TableViewTimeLine"];
    [self.tableView registerClass:[WD_TableViewTwo class] forCellReuseIdentifier:@"WD_TableViewTwo"];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.view addSubview:self.tableView];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.model = self.arrayDataSource[indexPath.row];
    
    if ([self.model.type isEqual:@10001]) {
        
        WD_TableViewTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_TableViewTwo"];
        
        WD_OwnerModel *modelOwner = self.nameArr[indexPath.row];
        cell.model = modelOwner;
        
        WD_RelatedModel *modelRelat = self.contentArr[indexPath.row];
        cell.modelRelate = modelRelat;
        
        //        WD_EpModel *modelE = self.arrayEp[indexPath.row];
        WD_EpModel *ep = modelRelat.eps;
        cell.modelEp = ep;
        //        NSLog(@"%lu", (unsigned long)self.arrayEp.count);
        [cell.imageViewAvatar sd_setImageWithURL:[NSURL URLWithString:self.arrayUrl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图_"]];
        
        
        return cell;
        
        
        
        
    }
    if([self.model.type isEqual:@10002] || [self.model.type isEqual:@10010]) {
        
        
        WD_TableViewTimeLine *cell = [tableView dequeueReusableCellWithIdentifier:@"WD_TableViewTimeLine"];
        
        WD_OwnerModel *modelOwner = self.nameArr[indexPath.row];
        self.modelRelat = self.contentArr[indexPath.row];
        
        cell.model = modelOwner;
        cell.modelRelate = self.modelRelat;
#pragma block 方法调用
        
        // 弱引用
        __weak WD_TimeLineViewController *mySelf = self;
        cell.blockRow = ^(NSInteger num){
            
            mySelf.num = num;
            [mySelf giveNum];
        };
        
        
        
        cell.cellArray = [NSMutableArray arrayWithArray:self.mutable];
        
        [cell.imageViewAvatar sd_setImageWithURL:[NSURL URLWithString:self.arrayUrl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"占位图_"]];
        
        return cell;
    }
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    
    cell.backgroundColor = [UIColor redColor];
    
    return cell;
    
}

- (void)giveNum {
    WD_ShowImageViewController *showVC = [[WD_ShowImageViewController alloc] init];
    showVC.numImage = self.num;
    showVC.related = self.modelRelat;
    [self.navigationController pushViewController:showVC animated:YES];
}

// block 方法
- (void)showAction:(NSMutableArray *)array {
    
    
    WD_ShowImageViewController *showVC = [[WD_ShowImageViewController alloc] init];
    
    
    
    [self.navigationController pushViewController:showVC animated:YES];
    NSLog(@">>>>  %ld <<<<", self.num);
    //    [self addChildViewController:showVC];
    //
    //    [self.view addSubview:showVC.view];
    //    showVC.view.frame = self.view.bounds;
    //    showVC.view.backgroundColor = [UIColor redColor];
    
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayDataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.model.type isEqual:@10001]) {
        
        
        return 300;
    } else {
        
        
        return 500;
    }
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WD_ZiLiaoViewController *ziliaoVC = [[WD_ZiLiaoViewController alloc] init];
    
    [self.navigationController pushViewController:ziliaoVC animated:YES
     ];
    
}



- (void)creatData {
    self.arrayDataSource = [NSMutableArray arrayWithCapacity:0];
    self.strUrl = [[NSString alloc] init];
    self.arrayUrl = [NSMutableArray arrayWithCapacity:0];
    self.arrayEp = [NSMutableArray arrayWithCapacity:0];
    self.nameArr = [NSMutableArray arrayWithCapacity:0];
    self.contentArr = [NSMutableArray arrayWithCapacity:0];
    
    NSString *str = @"http://pudding.cc/api/v1/activity/popular?apiKey=yuki_ios&auth1=d5aa784ef84b47c10f10c11d0d865c40&brand=Apple&channelId=App Store&deviceKey=5ECC523F-AED1-495A-8FD7-8CDA3770F7BD&limit=20&model=iPhone&os=iOS&osv=8.1.3&timestamp=1459841521&types=10001,10002,10007,10009,10010,10011&version=2.7.8";
    
    
    
    
    
    
    [JQAFNetTool GETNetWithUrl:str body:nil headerFile:nil response:JQJSON success:^(id result) {
        if (result) {
            
            for (NSDictionary *dic in result) {
                
                WD_TimeLineModel *model = [[WD_TimeLineModel alloc] initWithDataSource:dic];
                [self.arrayDataSource addObject:model];
                [self.nameArr addObject:model.ownerObjects];
                if (model.relatedObjects) {
                    [self.contentArr addObject:model.relatedObjects];
                    //
                } else {
                    [self.arrayDataSource removeLastObject];
                }
                NSDictionary *dicOwnerObject = [dic objectForKey:@"ownerObject"];
                NSDictionary *dicAvatar = [dicOwnerObject objectForKey:@"avatar"];
                self.strUrl = [dicAvatar objectForKey:@"url"];
                [self.arrayUrl addObject:self.strUrl];
                
                WD_RelatedModel *relatedModel = model.relatedObjects;
                self.mutable = [NSMutableArray arrayWithCapacity:0];
                
                for (NSDictionary *dic in relatedModel.images) {
                    WD_imageModel *imageModel = [[WD_imageModel alloc] init];
                    [imageModel setValuesForKeysWithDictionary:dic];
                    [self.mutable addObject:imageModel];
                }
                
                
                //                [self.arrayImage addObject:model.relatedObjects];
                
                //                NSDictionary *dicRelate = [dic objectForKey:@"relatedObject"];
                
                //                NSDictionary *dicEp = [dicRelate objectForKey:@"ep"];
                //                NSLog(@"AAAAAAAA%@", dicEp);
                
                
                //                [self.arrayEp addObject:model.relatedObjects];
                
                
                
                
                
                
                
                
            }
            
            [self.tableView reloadData];
            
        }
        
        
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
